import 'package:flutter/material.dart';
import 'package:untitled1/app/res/myColors.dart';
import 'package:untitled1/app/widgets/TitleBar.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

import '../../../widgets/base_page/BaseStatefulPage.dart';
//消息列表
class MessageListPage extends BaseStatefulPage {
  @override
  BaseStatefulPageState<MessageListPage> createState() {
    return MessageListPageState();
  }
}

class MessageListPageState extends BaseStatefulPageState<MessageListPage> {
  //刷新控制器
  final RefreshController _refreshController = RefreshController(
    initialRefresh: false,
  );

  // 模拟下拉刷新
  void _onRefresh() async {
    // 模拟网络请求延时
    await Future.delayed(Duration(seconds: 2));
    // setState(() {
    //   //数据初始化
    //   data = List.generate(15, (index) => 'Refreshed Item $index');
    // });
    // 通知刷新完成
    _refreshController.refreshCompleted();
  }

  // 模拟上拉加载更多
  void _onLoading() async {
    // 模拟网络请求延时
    await Future.delayed(Duration(seconds: 2));
    // if (data.length >= 40) {
    //   // 没有更多数据
    //   _refreshController.loadNoData();
    // } else {
    // setState(() {
    //   //每次加载10条数据
    //   data.addAll(List.generate(10, (index) => 'Loaded Item ${data.length + index}'));
    // });
    // 加载完成
    _refreshController.loadComplete();
    // }
  }

  @override
  void dispose() {
    _refreshController.dispose(); // 释放资源
    super.dispose();
  }

  @override
  Widget buildPage(BuildContext context) {
    return Scaffold(
      backgroundColor: myColors.white,
      appBar: TitleBar(title: "消息"),
      body: SmartRefresher(
          controller: _refreshController,
          enablePullDown: true,
          // 开启下拉刷新
          enablePullUp: true,
          // 开启上拉加载
          onRefresh: _onRefresh,
          // 下拉刷新回调
          onLoading: _onLoading,
          // 上拉加载回调

          //头部样式
          header: ClassicHeader(
            refreshingText: "正在刷新...",
            idleText: "下拉可以刷新",
            completeText: "刷新完成",
            failedText: "刷新失败",
            releaseText: "松开立即刷新",
            textStyle: TextStyle(color: Colors.black87),
          ),
          // 自带经典尾部样式
          footer: ClassicFooter(
            noDataText: "没有更多数据",
            loadingText: "加载中...",
            idleText: "上拉加载更多",
            failedText: "加载失败，请重试",
          ),
          child: ListView.separated(
            itemCount: 20,
            // 总共显示 20 项
            scrollDirection: Axis.vertical,
            // 列表垂直滚动
            reverse: false,
            // 是否倒序，false 表示从上往下滚动
            controller: ScrollController(),
            // 控制滚动位置（非必须）
            primary: false,
            // 如果为 true，不允许同时设置 controller
            physics: const BouncingScrollPhysics(),
            // iOS 回弹效果
            shrinkWrap: false,
            // false 表示撑满父容器；嵌套场景设为 true
            keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
            separatorBuilder: (BuildContext context, int index) {
              return Container(
                height: 5,
                color: myColors.c_FBFBFB,
                width: MediaQuery.of(context).size.width,
              );
            },
            itemBuilder: (context, index) {
              return Container(
                color: myColors.white,
                constraints: BoxConstraints(
                  minHeight: 100, // 设置最小高度为 100
                ),

                child: Row(
                  children: [
                    Container(
                      margin: EdgeInsets.only(left: 10,right: 10),
                      constraints: BoxConstraints(
                        minHeight: 60,
                      ),
                      child: Image.asset(
                        'assets/app/logo.png',
                        width: 50,
                        height: 50,
                      ),
                    ),
                    Expanded(
                      child: Container(
                    margin: EdgeInsets.only(left: 10, right: 10),
                    child:  Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        SizedBox(height: 10),
                        Text(
                          '2025.06.30 14:22:40',
                          style: TextStyle(
                            fontSize: 14,
                            color: myColors.c_707070,
                          ),
                        ),
                        SizedBox(height: 10),
                        Text(
                          'xxxxxx嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻xxxxxx嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻xxxxxx嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻xxxxxx嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻xxxxxx嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻xxxxxx嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻xxxxxx嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻xxxxxx嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻',
                          style: TextStyle(
                            fontSize: 14,
                            color: myColors.c_191919,
                          ),
                        ),
                        SizedBox(height: 10),
                      ],
                    ),
                    ),


                    ),
                  ],
                ),
              );
            },

          ),
        ),
    );
  }
}
